﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Benchmark.Db.Base.DataCreation;
using System.Threading.Tasks;
using Benchmark.Db.Base.DataObjects;

namespace Benchmark.Db.Base.DataCreation
{
    public class RandomDataCreator<T>
        where T : class, IDataObject, new()
    {
        protected PropertyMapper<T> RandomTypeMap { get; set; }

        public RandomDataCreator()
        {
            Type type = typeof(T);
            this.RandomTypeMap = new PropertyMapper<T>();
        }

        private T Create()
        {
            T obj = new T();
            this.RandomTypeMap.SetRandomValues(obj);
            return obj;
        }

        public T[] Create(int count)
        {
            T[] array = new T[count];

            Parallel.For(0, count, i =>
            {
                array[i] = this.Create();
            });

            return array;
        }

        public T[] ChangeValues(T[] array)
        {
            Parallel.For(0, array.Length, i =>
            {
                object obj = array[i];
                this.RandomTypeMap.SetRandomValuesWithoutPrimaryKey(ref obj);
            });

            return array;
        }
    }
}
